package com.gduf.lsm.controller;

import com.gduf.lsm.common.Result;
import com.gduf.lsm.entity.Older;
import com.gduf.lsm.service.OlderService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 公告信息表前端操作接口
 **/
@RestController
@RequestMapping("/older")

public class OlderController {

    @Resource
    private OlderService olderService;

    /**
     * 新增
     * @param older
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Older older) {
        olderService.add(older);
        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        olderService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        olderService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     * @param older
     * @return
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Older older) {
        olderService.updateById(older);
        return Result.success();
    }

    /**
     * 体检数加一
     * @param oldId
     * @return
     */
    @PutMapping("/addPhyNum/{oldId}")
    public Result addPhyNum(@PathVariable Integer oldId){
        olderService.addPhyNum(oldId);
        return Result.success();
    }

    @PutMapping("/addCulNum/{oldId}")
    public Result addCulNum(@PathVariable Integer oldId){
        olderService.addCulNum(oldId);
        return Result.success();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Older older = olderService.selectById(id);
        return Result.success(older);
    }

    /**
     * 查询所有
     * @param older
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(Older older ) {
        List<Older> list = olderService.selectAll(older);
        return Result.success(list);
    }

    /**
     * 查找某用户的老人档案
     * @param userId
     * @return
     */
    @GetMapping("/selectByUserId/{userId}")
    public Result selectByUserId(@PathVariable Integer userId){
        List<Older> list=olderService.selectByUserId(userId);
        return Result.success(list);
    }

    /**
     * 分页查询
     * @param older
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectPage")
    public Result selectPage(Older older,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Older> page = olderService.selectPage(older, pageNum, pageSize);
        return Result.success(page);
    }

}